<?php

namespace app\admin\controller;

use app\admin\model\User;
use app\common\model\Download;    //下载方法公共类
use app\common\controller\Backend;
use PhpOffice\PhpWord\SimpleType\JcTable;
use think\Exception;
use app\common\model\TestDyznAnswer;
use app\common\model\TestHldAnswer;
use app\common\model\TestMbtiAnswer;
use app\common\model\TestZymAnswer;
use PhpOffice\PhpWord\PhpWord;

use app\admin\model\test\hld\Cate as hldCate;
use app\admin\model\test\mbti\Cate as mbtiCate;
use app\admin\model\test\zym\Cate as zymCate;
use app\admin\model\test\dyzn\Cate as dyznCate;
use think\Db;


use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Writer\PDF;

//use Dompdf\Dompdf as DompdfLib;
use PhpOffice\PhpWord\Writer\WriterInterface;
use PhpOffice\PhpWord\Writer\PDF\AbstractRenderer;
use PhpOffice\PhpWord\Writer\PDF\DomPDF;

use PhpOffice\PhpSpreadsheet\IOFactory as SpreadIoFactory;
use PhpOffice\PhpPresentation\IOFactory as PowerpointIoFactory;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Projects extends Backend
{
    
    /**
     * Projects模型对象
     * @var \app\admin\model\Projects
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Projects;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function links($ids = null){
        if(!$ids){
            return false;
        }
        $domain = $this -> request -> domain();
        $links = [
            'hld'   => $domain.'/index/testing/hldtest/project_id/'.$ids.'.html',
            'mbti'  => $domain.'/index/testing/mbtitest/project_id/'.$ids.'.html',
            'dyzn'  => $domain.'/index/testing/dyzntest/project_id/'.$ids.'.html',
            'zym'  => $domain.'/index/testing/zymtest/project_id/'.$ids.'.html',
        ];

        $this -> assign('links',$links);
        return $this -> fetch();
    }

    /**
     * 测评记录
     */
    public function records($ids = null){
        if(!$ids) $this -> error('重新打开');
        if($this -> request -> isAjax()){
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = (new User())
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = (new User())
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v)
            {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $this -> assignconfig('project_id',$ids);
        return $this -> fetch();
    }

    /**
     * 下载用户的所有测试记录
     * @param null $user_id
     * @param null $project_id
     */
    public function downrecord($user_id = null,$project_id=null){
        if(!$user_id || !$project_id){
            $this -> error('请重试');
        }

        var_dump($user_id,$project_id);

    }

    /**
     * word转pdf
     * @param string $wordPath
     * @param string $fileName
     */
    public function wordToPdf($wordPath = '',$fileName = 'output')
    {
        if(!file_exists($wordPath)){
            die("文件不存在");
        }
        // 设置具体存放路径
        $basePath = sprintf('%spublic/uploads/pdf/%s',ROOT_PATH,date('Ymd'));
        $path = sprintf('%s/%s.pdf',$basePath,$fileName);
        if (!file_exists($basePath)) {
            mkdir($basePath, 0755, true); //创建目录
        }
        // 创建一个新的 PHPWord 实例
        $phpWord = new PhpWord();

//        Settings::setPdfRendererPath(sprintf('%svendor/mpdf/mpdf',ROOT_PATH));
        Settings::setPdfRendererPath(sprintf('uploads/pdf/20250409/',ROOT_PATH));
        //设置转换pdf的类库名（DomPDF、MPDF、TCPDF）需要下载对应的类库
        Settings::setPdfRendererName('MPDF');
        // 从 Word 文档中加载内容
        $wordReader = IOFactory::createReader('Word2007');
        $phpWord = $wordReader->load($wordPath);
//        halt($path);

        // 用 PHPWord 创建 PDF 文件
        $xmlWriter = IOFactory::createWriter($phpWord, 'PDF');
        $xmlWriter->save($path);
        die('222');
//        if(file_exists($path)){
//            $pdf = new PDF();
//            $pngs = $pdf->pdfToPng($path);
//            if(count($pngs) > 0){
//                unlink($path);
//                return $pngs;
//            }
//            return [];
//        }else{
//            return [];
//        }
    }

    /**
     * 下载用户全部测试信息
     * made:cfx；
     * time：2020/09/03
     */
    public function download( $user_id, $project_id = '')//project_id由于项目业务暂不需要，所以功能暂不完善
    {
//        $file_name = 'uploads/20250408/4999.docx';
//        $fileName = 'wang';
//        $this->wordToPdf($file_name,$fileName);
//        die();

//        $filePath = "D:\phpstudy_pro\WWW\zengyi\schoolassessment\public\uploads\20250408-4999.docx'";
//        $filePath= iconv('UTF-8','gbk',$filePath);
//        $fileHandle=fopen($filePath,"rb");
//        if($fileHandle===false){
//            exit("Can not open file: 111");
//        }
//        //文件类型是:进制流。设置为utf8编码(支持中文文件名称)
//        header('Content-type:application/octet-stream; charset=utf-8');
//        header('Content-Description:File Transfer');//描述页面返回的结果
//        header('Content-Disposition: attachment; filename='.basename($filePath));//可以让浏览器弹出下载窗口
//        header('Content-Transfer-Encoding:binary');//内容编码方式，直接二进制，不要gzip压缩
//        header('Expires:0');//过期时间
//        header("Cache-Control:must-revalidate");
//        //制页面不缓存，作用与no-cache相同，但更严格，强制意味更明显缓存策略，
//        header('Pragma: public');
//        header('Content-Length:'.filesize($filePath));
//        ob_clean();
//        flush(); //清空缓冲区
//        readfile($filePath);
//        die();
//        // 加载Word文档
//        $document = IOFactory::load('uploads/20250408/-4999.docx');
//        $html = $document->save('html'); // 保存为HTML格式
//
//        // 创建Dompdf实例并加载HTML内容
//        $dompdf = new DomPDF();
//        $dompdf->loadHtml('uploads/20250408/-4999.docx');
//
//            // (可选) 设置纸张大小、方向等
////        $dompdf->setPaper('A4', 'portrait');
//
//        // 渲染PDF并输出或保存文件
//        $dompdf->render();
//        $output = $dompdf->output(); // 获取输出内容
//        file_put_contents('output.pdf', $output); // 保存为文件
//        die();
//        Settings::setPdfRendererName(Settings::PDF_RENDERER_TCPDF);
//        $phpWord = IOFactory::load('uploads/20250408/-4999.docx');
//        $pdfWriter = Pdf::class;
//        $pdfWriter = IOFactory::createWriter($phpWord, $pdfWriter);
//        $pdfWriter->save('demo.pdf');
//        die();
//        $file_name = 'uploads/20250408/-4999.docx';
//        $phpWord1 = IOFactory::load($file_name);
//
////        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord1, 'Word2007');
//        $pdfWriter = IOFactory::createWriter($phpWord1, 'Word2007');
//
//        $rand = rand(1111,9999);
//        $file_name = 'uploads/'.date('Ymd').'/'.$userInfo['mobile'].'-'.$rand.'.docx';
//        $tmpPath ='uploads/'.date('Ymd').'/';
//        if(!file_exists($tmpPath)){
//            mkdir($tmpPath, 0777, true);
//        }
//        $objWriter->save($file_name);
//
//        halt($phpWord1);
//        $pdfWriter = Pdf::class;
//        $pdfWriter = IOFactory::createWriter($phpWord1, 'PDF');
////        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'pdf');
//
//        $pdfWriter->save('demo.pdf');
//        die();
        try {
            if (empty($user_id) || $user_id == '')       //判断必传值是否存在
                $this->error('未获取到用户标识！');
            $download = new Download();                     //实例化Model层准备调用需要用到的方法
            $userInfo = $download->getUserInfo($user_id);//获取用户信息

            $array = [                                   //组装需要数组(PS:为之后做压缩文件做准备)
                'user_id' => $user_id,
                'project_id' => $project_id
            ];

            $model = $download->instantiate(true, true, true, true);       //实例化对应模型

            $rows = $download->getRows($array['user_id'], $model);                             //获取用户测试数据

            if (!$rows['dyzncs'] && !$rows['hldcs'] && !$rows['zymcs'] && !$rows['mbtics'])
                $this->error('该用户尚未进行任何测试');


            $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
            $phpWord = new \PhpOffice\PhpWord\PhpWord();
            $phpWord->getSettings()->setThemeFontLang($languageEnGb);
            //$phpWord->getSettings()->setDefaultPager('B5');
            //$section = $phpWord->addSection();


            $sectionX = $phpWord->addSection([
                'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(10.7),
                'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
            ]);
            $imgUrl = ROOT_PATH . 'public/assets/img/xiao.png'; //获取封面URL地址
            $botImgUrl = ROOT_PATH . 'public/assets/img/cp3.png';
            $bodyUrl = ROOT_PATH . 'public/assets/img/body.png';
            $xianUrl = ROOT_PATH . 'public/assets/img/hengxian.png';

            $sectionX->addImage(
                $imgUrl,
                array(
                    'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(6.5),
                    'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.3),
                    'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'marginLeft' => '230',
                    'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-1.2),
                )
            );

            $sectionX->addImage(
                $bodyUrl,
                array(
                    'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.6),
                    'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.7),
                    'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'marginLeft' => '130',
                    'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.2),
                )
            );

            $sectionX->addImage(
                $botImgUrl,
                array(
                    'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16)),
                    'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(20.5)),
                    'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE,
                    'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                    'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                    'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
//                    'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(20)),
//                    'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
//                    'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                    'wrappingStyle' => 'behind'
                )
            );

            $sectionX->addText('生 涯 测 评 报 告',
                [
                    'size' => 32,
                    'bold' => true,
                    'name' => '黑体',
                    'color' => '#0674bc',
                ], [
//			           'alignment'   => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,              //居中校准
                    'spaceAfter' => 2,                                                   //标题后部预留长度
                    'spaceBefore' => 4300,                                                    //标题前部预留长度
                ]
            );
            // 横线
            $sectionX->addImage(
                $xianUrl,
                array(
                    'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(8.5),
                    'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0.115),
                    'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'marginLeft' => -10,
                    'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0.05),
                    'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(8)),
                )
            );

            $TableContentStyleX = ['name' => '微软雅黑', 'size' => 11, 'lineHeight' => 1,'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER];
            $fancyTableStyleName = 'Fancy Table';
            $fancyTableStyle = array('borderSize' => 1,'borderColor'=>'#ffffff','alignment' => JcTable::START);
            $fancyTableFirstRowStyle = array('borderBottomSize' => 0.5);
            $fancyTableCellStyle = array('valign' => 'center');
            $fancyTableFontStyle = array('alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::START,'bold' => true);
            $fancyContentFontStyle = array('alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::START,'bold' => true);
            $phpWord->addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
            $table2 = $sectionX->addTable($fancyTableStyleName);
            $table2->addRow();
            if ($userInfo->gender == 1) {
                $gender = '男';
            } else{
                $gender = '女';
            }
            $table2->addCell(2400, $fancyTableCellStyle)->addText("姓名：".$userInfo->nickname,$TableContentStyleX, $fancyTableFontStyle);
            $table2->addCell(2400, $fancyTableCellStyle)->addText("性别：".$gender,$TableContentStyleX, $fancyTableFontStyle);
            $table2->addRow();
            $table2->addCell(2400, $fancyTableCellStyle)->addText("年级：".$userInfo->grade,$TableContentStyleX, $fancyTableFontStyle);
            $table2->addCell(2400, $fancyTableCellStyle)->addText("班级：".$userInfo->classed,$TableContentStyleX, $fancyTableFontStyle);

//			$blankSpace = '               ';
			$userInfoStyle = 'userinfo';
			switch ( $rows )                                                            //获取时间：通过判断获取用户已做过测试中的测试时间
			{
			    case ( !empty($rows['hldcs'])  && $rows['hldcs']-> create_time != '' ):
			        $time =$rows['hldcs']->create_time ? $rows['hldcs']->create_time : '' ;
			        break;
			    case ( !empty($rows['mbtics']) && $rows['mbtics']-> create_time != '' ):  //MBTI测试时间获取
			        $time = $rows['mbtics']->create_time ? $rows['mbtics']->create_time : '' ;
			        break;
			    case ( !empty($rows['dyzncs'])  && $rows[ 'dyzncs' ]-> create_time != '' ):  //多元化智能测试时间获取
			        $time = $rows[ 'dyzncs' ]->create_time ? $rows['dyzncs']->create_time : '' ;
			        break;
			    default:  //职业锚测试时间获取
			        $time = $rows['zymcs']->create_time ? $rows['zymcs']->create_time : '' ;
			        break;
			}
            $sectionX -> addText( ' 报告编号:'.date( 'Ymdhis', $time ).$userInfo['reliability'],[ 'size' => 14,'name' => '微软雅黑'], [
                'alignment'   => \PhpOffice\PhpWord\SimpleType\Jc::END,              //居中校准
                'left'=>400,
                'spaceAfter'  => 650,                                                   //标题后部预留长度
                'spaceBefore' => 4800,                                                    //标题前部预留长度
            ],$userInfoStyle );//添加用户信息


            //标题
            $titleStyle = ['name'=>'微软雅黑', 'size' => 16,'color'=>'#1b80ad'];
            $TableContentStyle = ['name'=>'微软雅黑', 'size' => 11,'lineHeight'=> 1];

            $section = $phpWord->addSection([
                  'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(10.7),
                  'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
            ]);
            $paragraphStyleName = 'pStyle';
            $phpWord->addParagraphStyle($paragraphStyleName, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,'spaceAfter' => 300,'spaceBefore' => 1200));
            $fontStyleName = 'rStyle';
            $phpWord->addFontStyle($fontStyleName, array('name'=>'微软雅黑', 'size' => 16, 'allCaps' => true));


            //目录
            $muluUrl = ROOT_PATH.'public/assets/img/cp8.png'; //目录封面
            $section->addImage(
                $muluUrl,
                array(
                    'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16)),
                    'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(22)),
                    'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                    'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                    'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                    'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                    'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                    'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                    'wrappingStyle' => 'behind'
                )
            );



            //标语和阅读建议
            $byUrl = ROOT_PATH.'public/assets/img/cp9.png'; //封面
            //报告标语 和 阅读建议
			$section2 = $phpWord-> addSection([
			      'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(12.7),
			      'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
			]);
            $section2->addImage(
                $byUrl,
                array(
                    'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                    'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(25)),
                    'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                    'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                    'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                    'marginLeft'       => '-10',
                    'wrappingStyle' => 'behind',
                )
            );


            $leftupUrl = ROOT_PATH.'public/assets/img/cp5.png'; //左上角封面
            $rightdownUrl = ROOT_PATH.'public/assets/img/cp6.png';//有下角封面


            // 霍兰德
            $hld = TestHldAnswer::get(['user_id'=>$user_id]);
            if($hld){
                $answers_ana = unserialize($hld->answers_ana);
                arsort($answers_ana);
                $r = array_slice($answers_ana,0,3);
                $str = '';
                foreach ($r as $k => $v){
                    $str .= $k;
                }

                $cates = hldCate::all();
                $cs = [];
                foreach ($cates as $k => $v){
                    $cs[$v['cate']] = $v['desc'];
                }
                $arr = ['r','c','e','s','a','i'];
//                $arr = ['r'=>'现实型 ( R )','c'=>'常规型 ( C )','e'=>'企业型 ( E )','s'=>'社会型 ( S )','a'=>'艺术型 ( A )','i'=>'研究型 ( I )'];
                $a = unserialize($hld -> answers_ana);
                arsort($a); // 降序排序
                $largest = array_slice($a, 0, 3,true); // 获取最大的四个值
                $a_string = '';
                foreach ($largest as $k => $v){
                    if($k=='r'){
                        $name = '现实型(R)';
                    }else if($k=='c'){
                        $name = '常规型(C)';
                    }else if($k=='e'){
                        $name = '企业型(E)';
                    }else if($k=='s'){
                        $name = '社会型(S)';
                    }else if($k=='a'){
                        $name = '艺术型(A)';
                    }else if($k=='i'){
                        $name = '研究型(I)';
                    }
                    $a_string = $a_string.$name.'+';
                }
                $ana = [];
                foreach ($arr as $v){
                    if(isset($a[$v]))
                        $ana[$v] = $a[$v];
                }

                $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
                $phpWord->getSettings()->setThemeFontLang($languageEnGb);
                $section3 = $phpWord->addSection([
                      'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(15.5),
                      'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3),
                ]);
                $section3->addText("三、霍兰德测评",$titleStyle,['alignment'=> \PhpOffice\PhpWord\SimpleType\JcTable::START,'spaceAfter'=>0,'spaceBefore'=>20]);
                $section3->addImage(
                    $leftupUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginLeft'       => '-20',
                        'marginTop'        => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-4),
                        'wrappingStyle' => 'behind',
                    )
                );
                $section3->addImage(
                    $rightdownUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                        'wrappingStyle' => 'behind'
                    )
                );

                $contentStyle = ['size'=>12,'name'=>'微软雅黑','color'=>'#333333','bold'=>true];
                $paragraphStyle = new \PhpOffice\PhpWord\Style\Paragraph();
                $paragraphStyle->setIndentation(['left' => 500, 'right' => 500]);

                $section3 -> addText('您的职业兴趣测评:',$contentStyle,$paragraphStyle);
                $section3 -> addText(''.strtoupper($str),['size'=>48,'name'=>'Calibri','bold' => true,'color'=>'#1b80ad'],$paragraphStyle);
                $section3 -> addText(substr($a_string,0,-1),$contentStyle,$paragraphStyle);

                if( !empty( $hld -> imgs ))
                {
                    $img = unserialize($hld -> imgs);
                    if(isset($img)){
                        $imgcount = count($img);
                        foreach ($img as $ig){
                            $imgpath = $_SERVER['DOCUMENT_ROOT'].$ig;
                            if(!is_file($imgpath)) continue;
                            $info = getimagesize($imgpath);
                            $oheight = $height = $info[1];
                            $owidth = $width = $info[0];
                            if($imgcount == 1){
                                $width = min($width,300);
                                // 图片高度太高了，调整图片高度
                                $height = intval($oheight * ($width/$owidth));
                                // $height = 300;
                            }
                            $section3->addImage($_SERVER['DOCUMENT_ROOT'].$ig, array('width'=>$width, 'height'=>$height,'alignment'=> \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
                        }
                    }
                }
                $html = str_replace(array("<br>","'","<o:p>","</o:p>","&quot;",'calibri=""','light";=""','font-size:=""','16pt;"=""'),'',$cs[$str]);
                \PhpOffice\PhpWord\Shared\Html::addHtml($section3, $html, false, false);
            }


            // MBTI
            $mbti = TestMbtiAnswer::get(['user_id'=>$user_id]);
            if($mbti){
                $cates = mbtiCate::all();
                $cs1 = [];
                foreach ($cates as $k => $v){
                    $cs1[$v['cate']] = $v['desc'];
                }
                $answers_ana = unserialize($mbti->answers_ana);

                $ae = isset($answers_ana['e']) ? $answers_ana['e'] : 0;
                $ai = isset($answers_ana['i']) ? $answers_ana['i'] : 0;
                $an = isset($answers_ana['n']) ? $answers_ana['n'] : 0;
                $as = isset($answers_ana['s']) ? $answers_ana['s'] : 0;
                $at = isset($answers_ana['t']) ? $answers_ana['t'] : 0;
                $af = isset($answers_ana['f']) ? $answers_ana['f'] : 0;
                $aj = isset($answers_ana['j']) ? $answers_ana['j'] : 0;
                $ap = isset($answers_ana['p']) ? $answers_ana['p'] : 0;
                $e = [
                    'count' => $ae,
                    'percent'   => round($ae/($ae+$ai),2),
                    'name'  => '外向'
                ];
                $i = [
                    'count' => $ai,
                    'percent'   => 1- $e['percent'],
                    'name'  => '内向'
                ];
                $n = [
                    'count' => $an,
                    'percent'   => round($an/($an+$as),2),
                    'name'  => '直觉'
                ];
                $s = [
                    'count' => $as,
                    'percent'   => 1-$n['percent'],
                    'name'  => '感觉'
                ];
                $t = [
                    'count' => $at,
                    'percent'   => round($at/($at+$af),2),
                    'name'  => '思考'
                ];
                $f = [
                    'count' => $af,
                    'percent'   => 1-$t['percent'],
                    'name'  => '情感'
                ];
                $j = [
                    'count' => $aj,
                    'percent'   => round($aj/($aj+$ap),2),
                    'name'  => '判断'
                ];
                $p = [
                    'count' => $ap,
                    'percent'   => 1 - $j['percent'],
                    'name'  => '感知'
                ];

                $str1 = '';
                if($ai>=$ae){
                    $str1 .= 'i';
                }else{
                    $str1 .= 'e';
                }
                if($an>=$as){
                    $str1 .= 'n';
                }else{
                    $str1 .= 's';
                }

                if($af>=$at){
                    $str1 .= 'f';
                }else{
                    $str1 .= 't';
                }

                if($ap>=$aj){
                    $str1 .= 'p';
                }else{
                    $str1 .= 'j';
                }

                $retArr = [
                    'e' => $e,
                    'i' => $i,
                    'n' => $n,
                    's' => $s,
                    'j' => $j,
                    'p' => $p,
                    'f' => $f,
                    't' => $t
                ];

                //排序获取mbti 性格类型文字
                $vaccineCount = array_column($retArr, 'count');
                array_multisort($vaccineCount ,SORT_DESC,$retArr);
                $largestFour = array_slice($retArr, 0, 4); // 获取最大的四个值
                $ret_string = '';
                foreach ($largestFour as $k => $v){
                    $ret_string = $ret_string.$v['name'].'+';
                }
                if($mbti -> imgs){
                    $img = unserialize($mbti -> imgs);
                }

                $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
                $phpWord->getSettings()->setThemeFontLang($languageEnGb);
                $section4 = $phpWord->addSection([
                     'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(14.7),
                     'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
                ]);
                $section4->addText("四、MBTI性格测评",$titleStyle,['alignment'=> \PhpOffice\PhpWord\SimpleType\JcTable::START,'spaceAfter'=>0,'spaceBefore'=>20]);
                $section4->addImage(
                    $leftupUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginLeft'       => '-20',
                        'marginTop'        => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-4),
                        'wrappingStyle' => 'behind',
                    )
                );
                $section4->addImage(
                    $rightdownUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                        'wrappingStyle' => 'behind'
                    )
                );
                $contentStyle = ['size'=>12,'name'=>'微软雅黑','color'=>'#333333','bold'=>true];
                $paragraphStyle = new \PhpOffice\PhpWord\Style\Paragraph();
                $paragraphStyle->setIndentation(['left' => 300, 'right' => 300]);

//                $section4 -> addText('您的性格类型:',$contentStyle,$paragraphStyle);
//                $section4 -> addText(''.strtoupper($str1),['size'=>48,'name'=>'Calibri','bold' => true,'color'=>'#1b80ad'],$paragraphStyle);
//                $section4 -> addText('('.substr($ret_string,0,-1).')',$contentStyle,$paragraphStyle);

                //各类型的图片
                $typepic = ROOT_PATH.'public/assets/img/'.strtoupper($str1).'.png';
                //各类型特征
                $explain = $cates = mbtiCate::where(['cate'=>$str1])->value('explain');

                $fancyTableStyleName = 'Fancy Table';
//                $fancyTableStyle = array('borderSize' => 1, 'borderColor' => '000000', 'cellMargin' => 30, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 0);
                $fancyTableStyle = array('borderSize' => 0, 'borderColor' => '#ebf1ef', 'borderTopSize' => 0, 'borderTopColor' => '000000', 'borderBottomSize' => 0, 'borderBottomColor' => '000000', 'borderLeftSize' => 0, 'borderLeftColor' => '000000', 'borderRightSize' => 0, 'borderRightColor' => '000000');
                $fancyTableFirstRowStyle= array('borderBottomSize' => 100, 'borderBottomColor' => '000000', 'bgColor' => '000000');
                $fancyTableCellStyle = array('valign' => 'center');

                $phpWord-> addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
                $table4 = $section4->addTable($fancyTableStyleName);
                $table4->addRow(400);

                $cell1 = $table4->addCell(4000, $fancyTableCellStyle); // 第一列，宽度
                $cell2 = $table4->addCell(5500, $fancyTableCellStyle); // 第一列，宽度
                //第一列的内容
                $cell1 -> addText('您的性格类型:',$contentStyle,$paragraphStyle);
                $cell1 -> addText(''.strtoupper($str1),['size'=>48,'name'=>'Calibri','bold' => true,'color'=>'#1b80ad'],$paragraphStyle);
                $cell1 -> addText('('.substr($ret_string,0,-1).')',$contentStyle,$paragraphStyle);
                //第二列的内容
                $cell2->addImage($typepic,array('width'=>95, 'height'=>95,'alignment'=> \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
                $cell2->addText($explain,['size'=>9,'name'=>'微软雅黑','color'=>'#333333','alignment'=> \PhpOffice\PhpWord\SimpleType\Jc::END]);

                $section4->addTextBreak(2);

                if(isset($img)){
                    $imgcount = count($img);
                    foreach ($img as $ig){
                        $imgpath = $_SERVER['DOCUMENT_ROOT'].$ig;
                        if(!is_file($imgpath)) continue;
                        $info = getimagesize($imgpath);
                        $oheight = $height = $info[1];
                        $owidth = $width = $info[0];
                        if($imgcount == 1){
                            $width = min($width,460);
                            $height = intval($oheight * ($width/$owidth));
                        }
                        $section4->addImage($_SERVER['DOCUMENT_ROOT'].$ig, array('width'=>$width, 'height'=>$height,'alignment'=> \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
                    }
                }
                $section4->addTextBreak(1);
                $tr1 = [];
                $tr2 = [];
                $tr3 = [];
                $arr = ['e','i','s','n','t','f','j','p'];
                foreach($arr as $vv){
                    foreach($retArr as $k => $v){
                        if($k == $vv){
                            $tr1[] = $v['name'].' '.strtoupper($k);
                            $tr2[] = $v['count'];
                            $tr3[] = ($v['percent']*100).'%';
                        }
                    }
                }
//                $section4->addTextBreak(1);
//                $fancyTableStyleName = 'Fancy Table';
//                $fancyTableStyle = array('borderSize' => 1, 'borderColor' => '000000', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 0);
//                $fancyTableFirstRowStyle = array('borderBottomSize' => 18, 'borderBottomColor' => '000000', 'bgColor' => '000000');
//                $fancyTableCellStyle = array('valign' => 'center');
//                $fancyTableFontStyle = array('bold' => true);
//                $phpWord->addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
//                $table = $section4->addTable($fancyTableStyleName);
//                $section4 -> addText('MBTI 测评结果分析',[
//                    'size'    => 18,
//                    'name'=>'黑体',
//                ],['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,
//				'spaceAfter'  => 100,                                                   //标题后部预留长度
//				'spaceBefore' => 0]);
//                $cs1[$str1] = $cs1['intj'];
                $html1 = str_replace(array("<br>","'","<o:p>","</o:p>","&quot;",'calibri=""','light";=""','font-size:=""','16pt;"=""'),'',$cs1[$str1]);
//                $html = str_replace('你的类型是：','你的类型是：'.strtoupper($str),$html);
                // $html = '你的类型是：'.strtoupper($str);
//                $section4 -> addText('综合你在四个维度上的倾向，总体来说，你的类型是：'.strtoupper($str).'型',[ 'size' => 12,'name' => '宋体' ,'lineHeight'  => 1.5],['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,
//				'spaceAfter'  => 100,                                                   //标题后部预留长度
//				'spaceBefore' => 0]);//添加用户信息
                \PhpOffice\PhpWord\Shared\Html::addHtml($section4,$html1,false, false);
            }
            //mbti end

            // 多元智能
            $dyzn = TestDyznAnswer::get(['user_id'=>$user_id]);
            if($dyzn){
                $cates = dyznCate::all();
                $answers_ana = unserialize($dyzn->answers_ana);
                $assData = [
                    'cates' => $cates,
                    'answers_ana'   => $answers_ana
                ];
                $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
                $phpWord->getSettings()->setThemeFontLang($languageEnGb);

                $section5 = $phpWord->addSection([
                      'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(9.165),
                      'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
                ]);
                $section5->addText("五、多元智能测评",$titleStyle,['alignment'=> \PhpOffice\PhpWord\SimpleType\JcTable::START,'spaceAfter'=>0,'spaceBefore'=>20]);
                $section5->addImage(
                    $leftupUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginLeft'       => '-20',
                        'marginTop'        => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-4),
                        'wrappingStyle' => 'behind',
                    )
                );
                $section5->addImage(
                    $rightdownUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                        'wrappingStyle' => 'behind'
                    )
                );

                $str1 = ['成绩'];
                $str2 = ['得分'];
                $ana = [];
                $strong = '';
                $nomal = '';
                $weak = '';
                foreach($assData['cates'] as $k => $v){
                    $str1[] = $v['name'];
                    $str2[] = $assData['answers_ana'][$v['id']];
                    $ana[$v['id']] = [
                        'name'  => $v['name'],
                        'score' => $assData['answers_ana'][$v['id']]
                    ];
                    if($assData['answers_ana'][$v['id']] > 16){
                        $strong .= $v['name'].'+';
                    }elseif($assData['answers_ana'][$v['id']] > 8 && $assData['answers_ana'][$v['id']] <=16){
                        $nomal .= $v['name'].',';
                    }else{
                        $weak .= $v['name'].',';
                    }
                }
                $strong = trim($strong,'+');//最优能力
                $nomal = trim($nomal,',');//中等能力
                $weak = trim($weak,',');//弱项能力

                $contentStyle = ['size'=>12,'name'=>'微软雅黑','color'=>'#333333','bold'=>true];
                $paragraphStyle = new \PhpOffice\PhpWord\Style\Paragraph();
                $paragraphStyle->setIndentation(['left' => 500, 'right' => 500]);
                $section5 -> addText('您的能力优势:',$contentStyle,$paragraphStyle);
                $section5 -> addText($strong,['size'=>14,'name'=>'微软雅黑','lineheight'=>1.5,'bold' => true,'color'=>'#1b80ad'],$paragraphStyle);

                if( $dyzn-> imgs ){
                    $img = unserialize( $dyzn-> imgs );
                    if( isset($img) )
                    {
                        $imgcount = count( $img );
                        foreach ( $img as $ig )
                        {
                            $imgpath = $_SERVER['DOCUMENT_ROOT'].$ig;
                            if(!is_file($imgpath)) continue;
                            $info    = getimagesize($imgpath);
                            $oheight = $height = $info[1];
                            $owidth  = $width = $info[0];
                            if( $imgcount == 1 )
                            {
                                $width = min($width,480);
                                $height = intval($oheight * ($width/$owidth));
                            }
                            $section5-> addImage($_SERVER['DOCUMENT_ROOT'].$ig, array('width'=>$width, 'height'=>$height,'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER));
                        }
                    }
                }
				// $section4->  addPageBreak();
                $section5->addTextBreak(1);
                $desc = "在表格中，得分越高说明你的这项能力越优势，反之，得分越低说明你的这项能力越不足。";
//                if($strong){
//                    $desc .= "最优能力分别是：$strong ;";
//                }
//                if($nomal){
//                    $desc .= "中等能力分别是：$nomal ;";
//                }
//                if($weak){
//                    $desc .= "弱项能力分别是：$weak ;";
//                }
                $section5 -> addText($desc,['name'=>'仿宋','size'=>14,'lineHeight'  => 1.5],
                    ['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::LEFT,
                'spaceAfter'  => 0,                                                   //标题后部预留长度
                'spaceBefore' => 500]);
            }
            // 多元智能  end

            // 职业锚
            $zym = TestZymAnswer::get(['user_id'=>$user_id]);
            if($zym){
                $cates = zymCate::all();
                $answers_arr = [];
                $answers_ana = unserialize($zym->answers_ana);
                $assData = [
                    'cates' => $cates,
                    'answers_ana'   => $answers_ana
                ];

                arsort($answers_ana); // 降序排序
                $largestFour = array_slice($answers_ana, 0, 4,true); // 获取最大的四个值
                $ana_string = '';
                foreach ($largestFour as $k => $v){
                    $name = zymCate::where('id',$k)->value('titlename');
                    $ana_string = $ana_string.$name.'+';
                }
                if($zym -> imgs){
                    $img = unserialize($zym -> imgs);
                }
                $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
                $phpWord-> getSettings()-> setThemeFontLang($languageEnGb);
				$section6 = $phpWord->addSection([
				     'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(12.7),
				     'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
				]);
                $section6->addText("六、职业锚测评",$titleStyle, ['alignment'=> \PhpOffice\PhpWord\SimpleType\JcTable::START,'spaceAfter'=>0,'spaceBefore'=>20]);
                $section6->addImage(
                    $leftupUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginLeft'       => '-20',
                        'marginTop'        => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-4),
                        'wrappingStyle' => 'behind',
                    )
                );
                $section6->addImage(
                    $rightdownUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                        'wrappingStyle' => 'behind'
                    )
                );
                $contentStyle = ['size'=>12,'name'=>'微软雅黑','color'=>'#333333','bold'=>true];
                $paragraphStyle = new \PhpOffice\PhpWord\Style\Paragraph();
                $paragraphStyle->setIndentation(['left' => 500, 'right' => 500]);

                $section6 -> addText('您的职业观:',$contentStyle,$paragraphStyle);
                $section6->addTextBreak(1);
                $section6->addTextBreak(1);
                $section6 -> addText(substr($ana_string,0,-1),['size'=>14,'name'=>'微软雅黑','color'=>'#1b80ad','bold'=>true],$paragraphStyle);

                if(isset($img)){
                    $section6->addTextBreak(1);
                    $section6->addTextBreak(1);

                    $imgcount = count($img);
                    foreach ($img as $ig){
                        $imgpath = $_SERVER['DOCUMENT_ROOT'].$ig;
                        if(!is_file($imgpath)) continue;
                        $info = getimagesize($imgpath);
                        $oheight = $height = $info[1];
                        $owidth = $width = $info[0];
                        if($imgcount == 1)
                        {
                            $width = min($width,300);
                            $height = intval($oheight * ($width/$owidth));
                        }
                        $section6->addImage($_SERVER['DOCUMENT_ROOT'].$ig, array('width'=>$width, 'height'=>$height,'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER));
                    }

                    $section6->addTextBreak(1);
//                    $section6->addTextBreak(1);
//                    $section6->addTextBreak(1);

                    $fancyTableStyleName = 'Fancy Table';
                    $fancyTableStyle = array('borderSize' => 1, 'borderColor' => '000000', 'cellMargin' => 30, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 0);
                    $fancyTableFirstRowStyle = array('borderBottomSize' => 16, 'borderBottomColor' => '000000', 'bgColor' => '000000');
                    $fancyTableCellStyle = array('valign' => 'center', 'bgColor' => '#d2e3fb');
                    $fancyTableFontStyle = array('bold' => true,'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER);
                    $fancyContentFontStyle = array('alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER);


                    $TableContentStyle1 = array('size'=>12,'name'=>'微软雅黑','color'=>'#1b80ad');
                    $TableContentStyle2 = array('size'=>12,'name'=>'微软雅黑','color'=>'black');
                    $phpWord-> addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
                    $table2 = $section6->addTable($fancyTableStyleName);
                    $table2->addRow();
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("技术型(TF)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("生活型(LS)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("挑战型(CH)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("服务型(SV)",$TableContentStyle1, $fancyTableFontStyle);

                    $table2->addRow();
                    $table2->addCell(3500)->addText($answers_ana[1],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[8],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[7],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[6],$TableContentStyle2,$fancyContentFontStyle);

                    $table2->addRow();
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("创造型(EC)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("安全型(SE)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("自主型(AU)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("管理型(GM)",$TableContentStyle1, $fancyTableFontStyle);

                    $table2->addRow();
                    $table2->addCell(3500)->addText($answers_ana[5],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[4],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[3],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[2],$TableContentStyle2,$fancyContentFontStyle);
                }
				// $section6->  addPageBreak();
                $str1 = ['成绩'];
                $str2 = ['得分'];
                $ana = [];
                $strong = '';
                $nomal = '';
                $weak = '';
                foreach($assData['cates'] as $k => $v){
                    $str1[] = $v['name'];
                    $str2[] = $assData['answers_ana'][$v['id']];
                    $ana[$v['id']] = [
                        'name'  => $v['name'],
                        'score' => $assData['answers_ana'][$v['id']]
                    ];
                    if($assData['answers_ana'][$v['id']] > 16){
                        $strong .= $v['name'].',';
                    }elseif($assData['answers_ana'][$v['id']] > 8 && $assData['answers_ana'][$v['id']] <=16){
                        $nomal .= $v['name'].',';
                    }else{
                        $weak .= $v['name'].',';
                    }
                }
				$section7 = $phpWord->addSection([
				     'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(12.7),
				     'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
				]);
                $section7->addImage(
                    $leftupUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(3.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginLeft'       => '-20',
                        'marginTop'        => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-4),
                        'wrappingStyle' => 'behind',
                    )
                );
                $section7->addImage(
                    $rightdownUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(3.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                        'wrappingStyle' => 'behind'
                    )
                );
                $section7->addTextBreak(1);
                $fancyTableStyleName = 'Fancy Table';
                $fancyTableStyle = array('borderSize' => 0,'Spacing'=> 0,'borderColor' => '#ffffff', 'afterSpacing'=>0, 'cellMargin' => 0, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 0);
                $fancyTableFirstRowStyle = array('borderBottomSize' => 1, 'borderBottomColor' => '#ffffff');



                $fancyTableCellStyle = array('valign' => 'center');
                $fancyTableFontStyle = array('valign' => 'center','borderBottomSize'=>1,'borderBottomColor'=>'#ffffff','alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,'spaceAfter' => 50,'spaceBefore' =>50);
                $phpWord-> addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
                $table = $section7->addTable($fancyTableStyleName);

                $section7 -> addText('职业倾向测评分析详解',[
                    'size'    => 18,
                    'name'=>'黑体',
                ],['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,
				'spaceAfter'  => 200,                                                   //标题后部预留长度
				'spaceBefore' => 0]);
                $table = $section7->addTable($fancyTableStyleName);
                $table->addRow();

                // 定义首行单元格样式
                $cellFirstRowStyle  =  array(
                    'borderBottomColor' => '000000', // 下边框颜色
                    'borderBottomSize' => 5, // 下边框大小
                    'borderBottomStyle' => 'single', // 下边框样式
                );
                $table->addCell(2000, $cellFirstRowStyle )->addText("类型",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5], $fancyTableFontStyle);
                $table->addCell(7600, $cellFirstRowStyle )->addText("详解",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5], $fancyTableFontStyle);

                $table->addRow();
                $table->addCell(2000,$fancyTableCellStyle)->addText("技术型（TF）",['name'=>'仿宋','size'=>12,'lineHeight'  => 2.5]);
                $table->addCell(7600,$fancyTableCellStyle)->addText("愿意展示自己的技能，并不断提升自己的技术，发挥更为专业的能力。希望通过施展自己的技能以获取别人认可，并乐于接受来自于专业领域的挑战。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000,$fancyTableCellStyle)->addText("管理型（GM）",['name'=>'仿宋','size'=>12,'lineHeight'  => 2.5]);
                $table->addCell(7600,$fancyTableCellStyle)->addText("追求组织中的管理职位，喜欢发号施令，领导众人，因此能够整合其他人的工作，并对组织中某项工作的绩效承担责任。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000, $fancyTableCellStyle)->addText("自主型（AU）",['name'=>'仿宋','size'=>12,'lineHeight'  => 2.5]);
                $table->addCell(7600)->addText("希望可以按照自己的方式工作和生活，总能提供足够的灵活性，无法忍受公司种种的约束，追求无拘无束。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000, $fancyTableCellStyle)->addText("安全型（SE）",['name'=>'仿宋','size'=>12,'lineHeight'  => 2.5]);
                $table->addCell(7600, $fancyTableCellStyle)->addText("喜欢稳定的职位，更为强调财务安全和就业安全，关注五险一金、养老金以及税收。对组织忠心耿耿，可以长久地从事某一行业。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000, $fancyTableCellStyle)->addText("创造型（EC）",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5]);
                $table->addCell(7600, $fancyTableCellStyle)->addText("冒险能力强，手腕强硬，希望向周围的人证明你有能力创建一家企业，并且能够全方位的学习，以此来实现自己的创业计划。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000, $fancyTableCellStyle)->addText("服务型（SV）",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5]);
                $table->addCell(7600, $fancyTableCellStyle)->addText("追求职业的价值取向，希望可以做一些利于他人的事，例如教育、环保、公益、慈善等。富有爱心，安全感和同情心。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000, $fancyTableCellStyle)->addText("挑战型（CH）",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5]);
                $table->addCell(7600, $fancyTableCellStyle)->addText("总是跃跃欲试，希望战胜强硬的对手和克服面临的困难。多层次的、复杂的情况对你来说是一剂强心针，厌倦简单及重复的工作。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                // 定义尾行单元格样式
                $cellLastRowStyle  =  array(
                    'borderBottomColor' => '000000', // 下边框颜色
                    'borderBottomSize' => 5, // 下边框大小
                    'borderBottomStyle' => 'single', // 下边框样式
                    'valign' => 'center',
                );
                $table->addCell(2000, $cellLastRowStyle)->addText("生活型（LS）",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5]);
                $table->addCell(7600, $cellLastRowStyle)->addText("家庭的需求和你个人的休息对你来说非常重要，因此你希望职业有足够的弹性，能让你平衡并整合个人的、家庭的和职业的矛盾，并且让他们协调统一地共同发展。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);
            }
            // 职业锚  end

            $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');       
            $rand = rand(1111,9999);
//            $file_name = 'uploads/'.date('Ymd').'/'.$userInfo['mobile'].'-'.$rand.'.docx';
            $file_name = 'uploads/'.date('Ymd').'/'.$userInfo['grade'].'年级'.$userInfo['classed'].'班级'.$userInfo['nickname'].'.docx';
            $tmpPath ='uploads/'.date('Ymd').'/';
            if(!file_exists($tmpPath)){
                mkdir($tmpPath, 0777, true);
            }
            $objWriter->save($file_name);
            //以只读和二进制模式打开文件
            $file = fopen ( $file_name, "rb" );

            //告诉浏览器这是一个文件流格式的文件
            Header ( "Content-type: application/octet-stream" );
            //请求范围的度量单位
            Header ( "Accept-Ranges: bytes" );
            //Content-Length是指定包含于请求或响应中数据的字节长度
            Header ( "Accept-Length: " . filesize ( $file_name ) );
            //用来告诉浏览器，文件是可以当做附件被下载，下载后的文件名称为$file_name该变量的值。
            Header ( "Content-Disposition: attachment; filename=".$userInfo['grade'].'年级'.$userInfo['classed'].'班级'.$userInfo['nickname'].'.docx' );

            //读取文件内容并直接输出到浏览器
            echo fread ( $file, filesize ( $file_name ) );
            fclose ( $file );
            exit ();
        }
        catch (Exception $exception)
        {
            $this -> error($exception -> getMessage());
        }
    }

    /**
     * 批量生成
     * @param $user_id
     * @return void
     */
    public function generate($user_id){
        try
        {
            if( empty( $user_id ) || $user_id == '' )       //判断必传值是否存在
                $this -> error('未获取到用户标识！');
            $download = new Download();                     //实例化Model层准备调用需要用到的方法
            $userInfo = $download-> getUserInfo( $user_id );//获取用户信息

            $array    = [                                   //组装需要数组(PS:为之后做压缩文件做准备)
                'user_id'       => $user_id,
            ];

            $model = $download-> instantiate(true,true,true,true );       //实例化对应模型

            $rows  = $download-> getRows( $array[ 'user_id' ],$model );                             //获取用户测试数据

            if(!$rows['dyzncs'] && !$rows['hldcs'] && !$rows['zymcs'] && !$rows['mbtics'] )
                $this -> error('该用户尚未进行任何测试');


            $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
            $phpWord = new \PhpOffice\PhpWord\PhpWord();
            $phpWord->getSettings()->setThemeFontLang($languageEnGb);
            //$phpWord->getSettings()->setDefaultPager('B5');
            //$section = $phpWord->addSection();


            $sectionX = $phpWord->addSection([
                'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(10.7),
                'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
            ]);
            $imgUrl = ROOT_PATH . 'public/assets/img/xiao.png'; //获取封面URL地址
            $botImgUrl = ROOT_PATH . 'public/assets/img/cp3.png';
            $bodyUrl = ROOT_PATH . 'public/assets/img/body.png';
            $xianUrl = ROOT_PATH . 'public/assets/img/hengxian.png';

            $sectionX->addImage(
                $imgUrl,
                array(
                    'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(6.5),
                    'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.3),
                    'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'marginLeft' => '230',
                    'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-1.2),
                )
            );

            $sectionX->addImage(
                $bodyUrl,
                array(
                    'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.6),
                    'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.7),
                    'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'marginLeft' => '130',
                    'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.2),
                )
            );

            $sectionX->addImage(
                $botImgUrl,
                array(
                    'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16)),
                    'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(20.5)),
                    'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE,
                    'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                    'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                    'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
//                    'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(20)),
//                    'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
//                    'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                    'wrappingStyle' => 'behind'
                )
            );

            $sectionX->addText('生 涯 测 评 报 告',
                [
                    'size' => 32,
                    'bold' => true,
                    'name' => '黑体',
                    'color' => '#0674bc',
                ], [
//			           'alignment'   => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,              //居中校准
                    'spaceAfter' => 2,                                                   //标题后部预留长度
                    'spaceBefore' => 4300,                                                    //标题前部预留长度
                ]
            );
            // 横线
            $sectionX->addImage(
                $xianUrl,
                array(
                    'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(8.5),
                    'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0.115),
                    'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'marginLeft' => -10,
                    'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0.05),
                    'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(8)),
                )
            );

            $TableContentStyleX = ['name' => '微软雅黑', 'size' => 11, 'lineHeight' => 1,'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER];
            $fancyTableStyleName = 'Fancy Table';
            $fancyTableStyle = array('borderSize' => 1,'borderColor'=>'#ffffff','alignment' => JcTable::START);
            $fancyTableFirstRowStyle = array('borderBottomSize' => 0.5);
            $fancyTableCellStyle = array('valign' => 'center');
            $fancyTableFontStyle = array('alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::START,'bold' => true);
            $fancyContentFontStyle = array('alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::START,'bold' => true);
            $phpWord->addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
            $table2 = $sectionX->addTable($fancyTableStyleName);
            $table2->addRow();
            if ($userInfo->gender == 1) {
                $gender = '男';
            } else{
                $gender = '女';
            }
            $table2->addCell(2400, $fancyTableCellStyle)->addText("姓名：".$userInfo->nickname,$TableContentStyleX, $fancyTableFontStyle);
            $table2->addCell(2400, $fancyTableCellStyle)->addText("性别：".$gender,$TableContentStyleX, $fancyTableFontStyle);
            $table2->addRow();
            $table2->addCell(2400, $fancyTableCellStyle)->addText("年级：".$userInfo->grade,$TableContentStyleX, $fancyTableFontStyle);
            $table2->addCell(2400, $fancyTableCellStyle)->addText("班级：".$userInfo->classed,$TableContentStyleX, $fancyTableFontStyle);

//			$blankSpace = '               ';
            $userInfoStyle = 'userinfo';
            switch ( $rows )                                                            //获取时间：通过判断获取用户已做过测试中的测试时间
            {
                case ( !empty($rows['hldcs'])  && $rows['hldcs']-> create_time != '' ):
                    $time =$rows['hldcs']->create_time ? $rows['hldcs']->create_time : '' ;
                    break;
                case ( !empty($rows['mbtics']) && $rows['mbtics']-> create_time != '' ):  //MBTI测试时间获取
                    $time = $rows['mbtics']->create_time ? $rows['mbtics']->create_time : '' ;
                    break;
                case ( !empty($rows['dyzncs'])  && $rows[ 'dyzncs' ]-> create_time != '' ):  //多元化智能测试时间获取
                    $time = $rows[ 'dyzncs' ]->create_time ? $rows['dyzncs']->create_time : '' ;
                    break;
                default:  //职业锚测试时间获取
                    $time = $rows['zymcs']->create_time ? $rows['zymcs']->create_time : '' ;
                    break;
            }
            $sectionX -> addText( ' 报告编号:'.date( 'Ymdhis', $time ).$userInfo['reliability'],[ 'size' => 14,'name' => '微软雅黑'], [
                'alignment'   => \PhpOffice\PhpWord\SimpleType\Jc::END,              //居中校准
                'left'=>400,
                'spaceAfter'  => 650,                                                   //标题后部预留长度
                'spaceBefore' => 4800,                                                    //标题前部预留长度
            ],$userInfoStyle );//添加用户信息


            //标题
            $titleStyle = ['name'=>'微软雅黑', 'size' => 16,'color'=>'#1b80ad'];
            $TableContentStyle = ['name'=>'微软雅黑', 'size' => 11,'lineHeight'=> 1];

            $section = $phpWord->addSection([
                'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(10.7),
                'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
            ]);
            $paragraphStyleName = 'pStyle';
            $phpWord->addParagraphStyle($paragraphStyleName, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,'spaceAfter' => 300,'spaceBefore' => 1200));
            $fontStyleName = 'rStyle';
            $phpWord->addFontStyle($fontStyleName, array('name'=>'微软雅黑', 'size' => 16, 'allCaps' => true));


            //目录
            $muluUrl = ROOT_PATH.'public/assets/img/cp8.png'; //目录封面
            $section->addImage(
                $muluUrl,
                array(
                    'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16)),
                    'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(22)),
                    'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                    'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                    'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                    'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                    'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                    'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                    'wrappingStyle' => 'behind'
                )
            );



            //标语和阅读建议
            $byUrl = ROOT_PATH.'public/assets/img/cp9.png'; //封面
            //报告标语 和 阅读建议
            $section2 = $phpWord-> addSection([
                'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(12.7),
                'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
            ]);
            $section2->addImage(
                $byUrl,
                array(
                    'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                    'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(25)),
                    'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                    'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                    'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                    'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                    'marginLeft'       => '-10',
                    'wrappingStyle' => 'behind',
                )
            );


            $leftupUrl = ROOT_PATH.'public/assets/img/cp5.png'; //左上角封面
            $rightdownUrl = ROOT_PATH.'public/assets/img/cp6.png';//有下角封面


            // 霍兰德
            $hld = TestHldAnswer::get(['user_id'=>$user_id]);
            if($hld){
                $answers_ana = unserialize($hld->answers_ana);
                arsort($answers_ana);
                $r = array_slice($answers_ana,0,3);
                $str = '';
                foreach ($r as $k => $v){
                    $str .= $k;
                }

                $cates = hldCate::all();
                $cs = [];
                foreach ($cates as $k => $v){
                    $cs[$v['cate']] = $v['desc'];
                }
                $arr = ['r','c','e','s','a','i'];
//                $arr = ['r'=>'现实型 ( R )','c'=>'常规型 ( C )','e'=>'企业型 ( E )','s'=>'社会型 ( S )','a'=>'艺术型 ( A )','i'=>'研究型 ( I )'];
                $a = unserialize($hld -> answers_ana);
                arsort($a); // 降序排序
                $largest = array_slice($a, 0, 3,true); // 获取最大的四个值
                $a_string = '';
                foreach ($largest as $k => $v){
                    if($k=='r'){
                        $name = '现实型(R)';
                    }else if($k=='c'){
                        $name = '常规型(C)';
                    }else if($k=='e'){
                        $name = '企业型(E)';
                    }else if($k=='s'){
                        $name = '社会型(S)';
                    }else if($k=='a'){
                        $name = '艺术型(A)';
                    }else if($k=='i'){
                        $name = '研究型(I)';
                    }
                    $a_string = $a_string.$name.'+';
                }
                $ana = [];
                foreach ($arr as $v){
                    if(isset($a[$v]))
                        $ana[$v] = $a[$v];
                }

                $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
                $phpWord->getSettings()->setThemeFontLang($languageEnGb);
                $section3 = $phpWord->addSection([
                    'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(15.5),
                    'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3),
                ]);
                $section3->addText("三、霍兰德测评",$titleStyle,['alignment'=> \PhpOffice\PhpWord\SimpleType\JcTable::START,'spaceAfter'=>0,'spaceBefore'=>20]);
                $section3->addImage(
                    $leftupUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginLeft'       => '-20',
                        'marginTop'        => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-4),
                        'wrappingStyle' => 'behind',
                    )
                );
                $section3->addImage(
                    $rightdownUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                        'wrappingStyle' => 'behind'
                    )
                );

                $contentStyle = ['size'=>12,'name'=>'微软雅黑','color'=>'#333333','bold'=>true];
                $paragraphStyle = new \PhpOffice\PhpWord\Style\Paragraph();
                $paragraphStyle->setIndentation(['left' => 500, 'right' => 500]);

                $section3 -> addText('您的职业兴趣测评:',$contentStyle,$paragraphStyle);
                $section3 -> addText(''.strtoupper($str),['size'=>48,'name'=>'Calibri','bold' => true,'color'=>'#1b80ad'],$paragraphStyle);
                $section3 -> addText(substr($a_string,0,-1),$contentStyle,$paragraphStyle);

                if( !empty( $hld -> imgs ))
                {
                    $img = unserialize($hld -> imgs);
                    if(isset($img)){
                        $imgcount = count($img);
                        foreach ($img as $ig){
                            $imgpath = $_SERVER['DOCUMENT_ROOT'].$ig;
                            if(!is_file($imgpath)) continue;
                            $info = getimagesize($imgpath);
                            $oheight = $height = $info[1];
                            $owidth = $width = $info[0];
                            if($imgcount == 1){
                                $width = min($width,300);
                                // 图片高度太高了，调整图片高度
                                $height = intval($oheight * ($width/$owidth));
                                // $height = 300;
                            }
                            $section3->addImage($_SERVER['DOCUMENT_ROOT'].$ig, array('width'=>$width, 'height'=>$height,'alignment'=> \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
                        }
                    }
                }
                if(!empty($cs[$str])){
                    $html = str_replace(array("<br>","'","<o:p>","</o:p>","&quot;",'calibri=""','light";=""','font-size:=""','16pt;"=""'),'',$cs[$str]);
                    \PhpOffice\PhpWord\Shared\Html::addHtml($section3, $html, false, false);
                }
            }


            // MBTI
            $mbti = TestMbtiAnswer::get(['user_id'=>$user_id]);
            if($mbti){
                $cates = mbtiCate::all();
                $cs1 = [];
                foreach ($cates as $k => $v){
                    $cs1[$v['cate']] = $v['desc'];
                }
                $answers_ana = unserialize($mbti->answers_ana);

                $ae = isset($answers_ana['e']) ? $answers_ana['e'] : 0;
                $ai = isset($answers_ana['i']) ? $answers_ana['i'] : 0;
                $an = isset($answers_ana['n']) ? $answers_ana['n'] : 0;
                $as = isset($answers_ana['s']) ? $answers_ana['s'] : 0;
                $at = isset($answers_ana['t']) ? $answers_ana['t'] : 0;
                $af = isset($answers_ana['f']) ? $answers_ana['f'] : 0;
                $aj = isset($answers_ana['j']) ? $answers_ana['j'] : 0;
                $ap = isset($answers_ana['p']) ? $answers_ana['p'] : 0;
                $e = [
                    'count' => $ae,
                    'percent'   => round($ae/($ae+$ai),2),
                    'name'  => '外向'
                ];
                $i = [
                    'count' => $ai,
                    'percent'   => 1- $e['percent'],
                    'name'  => '内向'
                ];
                $n = [
                    'count' => $an,
                    'percent'   => round($an/($an+$as),2),
                    'name'  => '直觉'
                ];
                $s = [
                    'count' => $as,
                    'percent'   => 1-$n['percent'],
                    'name'  => '感觉'
                ];
                $t = [
                    'count' => $at,
                    'percent'   => round($at/($at+$af),2),
                    'name'  => '思考'
                ];
                $f = [
                    'count' => $af,
                    'percent'   => 1-$t['percent'],
                    'name'  => '情感'
                ];
                $j = [
                    'count' => $aj,
                    'percent'   => round($aj/($aj+$ap),2),
                    'name'  => '判断'
                ];
                $p = [
                    'count' => $ap,
                    'percent'   => 1 - $j['percent'],
                    'name'  => '感知'
                ];

                $str1 = '';
                if($ai>=$ae){
                    $str1 .= 'i';
                }else{
                    $str1 .= 'e';
                }
                if($an>=$as){
                    $str1 .= 'n';
                }else{
                    $str1 .= 's';
                }

                if($af>=$at){
                    $str1 .= 'f';
                }else{
                    $str1 .= 't';
                }

                if($ap>=$aj){
                    $str1 .= 'p';
                }else{
                    $str1 .= 'j';
                }

                $retArr = [
                    'e' => $e,
                    'i' => $i,
                    'n' => $n,
                    's' => $s,
                    'j' => $j,
                    'p' => $p,
                    'f' => $f,
                    't' => $t
                ];

                //排序获取mbti 性格类型文字
                $vaccineCount = array_column($retArr, 'count');
                array_multisort($vaccineCount ,SORT_DESC,$retArr);
                $largestFour = array_slice($retArr, 0, 4); // 获取最大的四个值
                $ret_string = '';
                foreach ($largestFour as $k => $v){
                    $ret_string = $ret_string.$v['name'].'+';
                }
                if($mbti -> imgs){
                    $img = unserialize($mbti -> imgs);
                }

                $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
                $phpWord->getSettings()->setThemeFontLang($languageEnGb);
                $section4 = $phpWord->addSection([
                    'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(14.7),
                    'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
                ]);
                $section4->addText("四、MBTI性格测评",$titleStyle,['alignment'=> \PhpOffice\PhpWord\SimpleType\JcTable::START,'spaceAfter'=>0,'spaceBefore'=>20]);
                $section4->addImage(
                    $leftupUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginLeft'       => '-20',
                        'marginTop'        => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-4),
                        'wrappingStyle' => 'behind',
                    )
                );
                $section4->addImage(
                    $rightdownUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                        'wrappingStyle' => 'behind'
                    )
                );
                $contentStyle = ['size'=>12,'name'=>'微软雅黑','color'=>'#333333','bold'=>true];
                $paragraphStyle = new \PhpOffice\PhpWord\Style\Paragraph();
                $paragraphStyle->setIndentation(['left' => 300, 'right' => 300]);

//                $section4 -> addText('您的性格类型:',$contentStyle,$paragraphStyle);
//                $section4 -> addText(''.strtoupper($str1),['size'=>48,'name'=>'Calibri','bold' => true,'color'=>'#1b80ad'],$paragraphStyle);
//                $section4 -> addText('('.substr($ret_string,0,-1).')',$contentStyle,$paragraphStyle);

                //各类型的图片
                $typepic = ROOT_PATH.'public/assets/img/'.strtoupper($str1).'.png';
                //各类型特征
                $explain = $cates = mbtiCate::where(['cate'=>$str1])->value('explain');

                $fancyTableStyleName = 'Fancy Table';
//                $fancyTableStyle = array('borderSize' => 1, 'borderColor' => '000000', 'cellMargin' => 30, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 0);
                $fancyTableStyle = array('borderSize' => 0, 'borderColor' => '#ebf1ef', 'borderTopSize' => 0, 'borderTopColor' => '000000', 'borderBottomSize' => 0, 'borderBottomColor' => '000000', 'borderLeftSize' => 0, 'borderLeftColor' => '000000', 'borderRightSize' => 0, 'borderRightColor' => '000000');
                $fancyTableFirstRowStyle= array('borderBottomSize' => 100, 'borderBottomColor' => '000000', 'bgColor' => '000000');
                $fancyTableCellStyle = array('valign' => 'center');

                $phpWord-> addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
                $table4 = $section4->addTable($fancyTableStyleName);
                $table4->addRow(400);

                $cell1 = $table4->addCell(4000, $fancyTableCellStyle); // 第一列，宽度
                $cell2 = $table4->addCell(5500, $fancyTableCellStyle); // 第一列，宽度
                //第一列的内容
                $cell1 -> addText('您的性格类型:',$contentStyle,$paragraphStyle);
                $cell1 -> addText(''.strtoupper($str1),['size'=>48,'name'=>'Calibri','bold' => true,'color'=>'#1b80ad'],$paragraphStyle);
                $cell1 -> addText('('.substr($ret_string,0,-1).')',$contentStyle,$paragraphStyle);
                //第二列的内容
                $cell2->addImage($typepic,array('width'=>95, 'height'=>95,'alignment'=> \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
                $cell2->addText($explain,['size'=>9,'name'=>'微软雅黑','color'=>'#333333','alignment'=> \PhpOffice\PhpWord\SimpleType\Jc::END]);

                $section4->addTextBreak(2);

                if(isset($img)){
                    $imgcount = count($img);
                    foreach ($img as $ig){
                        $imgpath = $_SERVER['DOCUMENT_ROOT'].$ig;
                        if(!is_file($imgpath)) continue;
                        $info = getimagesize($imgpath);
                        $oheight = $height = $info[1];
                        $owidth = $width = $info[0];
                        if($imgcount == 1){
                            $width = min($width,460);
                            $height = intval($oheight * ($width/$owidth));
                        }
                        $section4->addImage($_SERVER['DOCUMENT_ROOT'].$ig, array('width'=>$width, 'height'=>$height,'alignment'=> \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
                    }
                }
                $section4->addTextBreak(1);
                $tr1 = [];
                $tr2 = [];
                $tr3 = [];
                $arr = ['e','i','s','n','t','f','j','p'];
                foreach($arr as $vv){
                    foreach($retArr as $k => $v){
                        if($k == $vv){
                            $tr1[] = $v['name'].' '.strtoupper($k);
                            $tr2[] = $v['count'];
                            $tr3[] = ($v['percent']*100).'%';
                        }
                    }
                }
//                $section4->addTextBreak(1);
//                $fancyTableStyleName = 'Fancy Table';
//                $fancyTableStyle = array('borderSize' => 1, 'borderColor' => '000000', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 0);
//                $fancyTableFirstRowStyle = array('borderBottomSize' => 18, 'borderBottomColor' => '000000', 'bgColor' => '000000');
//                $fancyTableCellStyle = array('valign' => 'center');
//                $fancyTableFontStyle = array('bold' => true);
//                $phpWord->addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
//                $table = $section4->addTable($fancyTableStyleName);
//                $section4 -> addText('MBTI 测评结果分析',[
//                    'size'    => 18,
//                    'name'=>'黑体',
//                ],['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,
//				'spaceAfter'  => 100,                                                   //标题后部预留长度
//				'spaceBefore' => 0]);
//                $cs1[$str1] = $cs1['intj'];
                $html1 = str_replace(array("<br>","'","<o:p>","</o:p>","&quot;",'calibri=""','light";=""','font-size:=""','16pt;"=""'),'',$cs1[$str1]);
//                $html = str_replace('你的类型是：','你的类型是：'.strtoupper($str),$html);
                // $html = '你的类型是：'.strtoupper($str);
//                $section4 -> addText('综合你在四个维度上的倾向，总体来说，你的类型是：'.strtoupper($str).'型',[ 'size' => 12,'name' => '宋体' ,'lineHeight'  => 1.5],['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,
//				'spaceAfter'  => 100,                                                   //标题后部预留长度
//				'spaceBefore' => 0]);//添加用户信息
                \PhpOffice\PhpWord\Shared\Html::addHtml($section4,$html1,false, false);
            }
            //mbti end

            // 多元智能
            $dyzn = TestDyznAnswer::get(['user_id'=>$user_id]);
            if($dyzn){
                $cates = dyznCate::all();
                $answers_ana = unserialize($dyzn->answers_ana);
                $assData = [
                    'cates' => $cates,
                    'answers_ana'   => $answers_ana
                ];
                $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
                $phpWord->getSettings()->setThemeFontLang($languageEnGb);

                $section5 = $phpWord->addSection([
                    'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(9.165),
                    'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
                ]);
                $section5->addText("五、多元智能测评",$titleStyle,['alignment'=> \PhpOffice\PhpWord\SimpleType\JcTable::START,'spaceAfter'=>0,'spaceBefore'=>20]);
                $section5->addImage(
                    $leftupUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginLeft'       => '-20',
                        'marginTop'        => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-4),
                        'wrappingStyle' => 'behind',
                    )
                );
                $section5->addImage(
                    $rightdownUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                        'wrappingStyle' => 'behind'
                    )
                );

                $str1 = ['成绩'];
                $str2 = ['得分'];
                $ana = [];
                $strong = '';
                $nomal = '';
                $weak = '';
                foreach($assData['cates'] as $k => $v){
                    $str1[] = $v['name'];
                    $str2[] = $assData['answers_ana'][$v['id']];
                    $ana[$v['id']] = [
                        'name'  => $v['name'],
                        'score' => $assData['answers_ana'][$v['id']]
                    ];
                    if($assData['answers_ana'][$v['id']] > 16){
                        $strong .= $v['name'].'+';
                    }elseif($assData['answers_ana'][$v['id']] > 8 && $assData['answers_ana'][$v['id']] <=16){
                        $nomal .= $v['name'].',';
                    }else{
                        $weak .= $v['name'].',';
                    }
                }
                $strong = trim($strong,'+');//最优能力
                $nomal = trim($nomal,',');//中等能力
                $weak = trim($weak,',');//弱项能力

                $contentStyle = ['size'=>12,'name'=>'微软雅黑','color'=>'#333333','bold'=>true];
                $paragraphStyle = new \PhpOffice\PhpWord\Style\Paragraph();
                $paragraphStyle->setIndentation(['left' => 500, 'right' => 500]);
                $section5 -> addText('您的能力优势:',$contentStyle,$paragraphStyle);
                $section5 -> addText($strong,['size'=>14,'name'=>'微软雅黑','lineheight'=>1.5,'bold' => true,'color'=>'#1b80ad'],$paragraphStyle);

                if( $dyzn-> imgs ){
                    $img = unserialize( $dyzn-> imgs );
                    if( isset($img) )
                    {
                        $imgcount = count( $img );
                        foreach ( $img as $ig )
                        {
                            $imgpath = $_SERVER['DOCUMENT_ROOT'].$ig;
                            if(!is_file($imgpath)) continue;
                            $info    = getimagesize($imgpath);
                            $oheight = $height = $info[1];
                            $owidth  = $width = $info[0];
                            if( $imgcount == 1 )
                            {
                                $width = min($width,480);
                                $height = intval($oheight * ($width/$owidth));
                            }
                            $section5-> addImage($_SERVER['DOCUMENT_ROOT'].$ig, array('width'=>$width, 'height'=>$height,'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER));
                        }
                    }
                }
                // $section4->  addPageBreak();
                $section5->addTextBreak(1);
                $desc = "在表格中，得分越高说明你的这项能力越优势，反之，得分越低说明你的这项能力越不足。";
//                if($strong){
//                    $desc .= "最优能力分别是：$strong ;";
//                }
//                if($nomal){
//                    $desc .= "中等能力分别是：$nomal ;";
//                }
//                if($weak){
//                    $desc .= "弱项能力分别是：$weak ;";
//                }
                $section5 -> addText($desc,['name'=>'仿宋','size'=>14,'lineHeight'  => 1.5],
                    ['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::LEFT,
                        'spaceAfter'  => 0,                                                   //标题后部预留长度
                        'spaceBefore' => 500]);
            }
            // 多元智能  end

            // 职业锚
            $zym = TestZymAnswer::get(['user_id'=>$user_id]);
            if($zym){
                $cates = zymCate::all();
                $answers_arr = [];
                $answers_ana = unserialize($zym->answers_ana);
                $assData = [
                    'cates' => $cates,
                    'answers_ana'   => $answers_ana
                ];

                arsort($answers_ana); // 降序排序
                $largestFour = array_slice($answers_ana, 0, 4,true); // 获取最大的四个值
                $ana_string = '';
                foreach ($largestFour as $k => $v){
                    $name = zymCate::where('id',$k)->value('titlename');
                    $ana_string = $ana_string.$name.'+';
                }
                if($zym -> imgs){
                    $img = unserialize($zym -> imgs);
                }
                $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
                $phpWord-> getSettings()-> setThemeFontLang($languageEnGb);
                $section6 = $phpWord->addSection([
                    'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(12.7),
                    'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
                ]);
                $section6->addText("六、职业锚测评",$titleStyle, ['alignment'=> \PhpOffice\PhpWord\SimpleType\JcTable::START,'spaceAfter'=>0,'spaceBefore'=>20]);
                $section6->addImage(
                    $leftupUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginLeft'       => '-20',
                        'marginTop'        => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-4),
                        'wrappingStyle' => 'behind',
                    )
                );
                $section6->addImage(
                    $rightdownUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                        'wrappingStyle' => 'behind'
                    )
                );
                $contentStyle = ['size'=>12,'name'=>'微软雅黑','color'=>'#333333','bold'=>true];
                $paragraphStyle = new \PhpOffice\PhpWord\Style\Paragraph();
                $paragraphStyle->setIndentation(['left' => 500, 'right' => 500]);

                $section6 -> addText('您的职业观:',$contentStyle,$paragraphStyle);
                $section6->addTextBreak(1);
                $section6->addTextBreak(1);
                $section6 -> addText(substr($ana_string,0,-1),['size'=>14,'name'=>'微软雅黑','color'=>'#1b80ad','bold'=>true],$paragraphStyle);

                if(isset($img)){
                    $section6->addTextBreak(1);
                    $section6->addTextBreak(1);

                    $imgcount = count($img);
                    foreach ($img as $ig){
                        $imgpath = $_SERVER['DOCUMENT_ROOT'].$ig;
                        if(!is_file($imgpath)) continue;
                        $info = getimagesize($imgpath);
                        $oheight = $height = $info[1];
                        $owidth = $width = $info[0];
                        if($imgcount == 1)
                        {
                            $width = min($width,300);
                            $height = intval($oheight * ($width/$owidth));
                        }
                        $section6->addImage($_SERVER['DOCUMENT_ROOT'].$ig, array('width'=>$width, 'height'=>$height,'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER));
                    }

                    $section6->addTextBreak(1);
//                    $section6->addTextBreak(1);
//                    $section6->addTextBreak(1);

                    $fancyTableStyleName = 'Fancy Table';
                    $fancyTableStyle = array('borderSize' => 1, 'borderColor' => '000000', 'cellMargin' => 30, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 0);
                    $fancyTableFirstRowStyle = array('borderBottomSize' => 16, 'borderBottomColor' => '000000', 'bgColor' => '000000');
                    $fancyTableCellStyle = array('valign' => 'center', 'bgColor' => '#d2e3fb');
                    $fancyTableFontStyle = array('bold' => true,'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER);
                    $fancyContentFontStyle = array('alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER);


                    $TableContentStyle1 = array('size'=>12,'name'=>'微软雅黑','color'=>'#1b80ad');
                    $TableContentStyle2 = array('size'=>12,'name'=>'微软雅黑','color'=>'black');
                    $phpWord-> addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
                    $table2 = $section6->addTable($fancyTableStyleName);
                    $table2->addRow();
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("技术型(TF)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("生活型(LS)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("挑战型(CH)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("服务型(SV)",$TableContentStyle1, $fancyTableFontStyle);

                    $table2->addRow();
                    $table2->addCell(3500)->addText($answers_ana[1],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[8],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[7],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[6],$TableContentStyle2,$fancyContentFontStyle);

                    $table2->addRow();
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("创造型(EC)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("安全型(SE)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("自主型(AU)",$TableContentStyle1, $fancyTableFontStyle);
                    $table2->addCell(3500, $fancyTableCellStyle)->addText("管理型(GM)",$TableContentStyle1, $fancyTableFontStyle);

                    $table2->addRow();
                    $table2->addCell(3500)->addText($answers_ana[5],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[4],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[3],$TableContentStyle2,$fancyContentFontStyle);
                    $table2->addCell(3500)->addText($answers_ana[2],$TableContentStyle2,$fancyContentFontStyle);
                }
                // $section6->  addPageBreak();
                $str1 = ['成绩'];
                $str2 = ['得分'];
                $ana = [];
                $strong = '';
                $nomal = '';
                $weak = '';
                foreach($assData['cates'] as $k => $v){
                    $str1[] = $v['name'];
                    $str2[] = $assData['answers_ana'][$v['id']];
                    $ana[$v['id']] = [
                        'name'  => $v['name'],
                        'score' => $assData['answers_ana'][$v['id']]
                    ];
                    if($assData['answers_ana'][$v['id']] > 16){
                        $strong .= $v['name'].',';
                    }elseif($assData['answers_ana'][$v['id']] > 8 && $assData['answers_ana'][$v['id']] <=16){
                        $nomal .= $v['name'].',';
                    }else{
                        $weak .= $v['name'].',';
                    }
                }
                $section7 = $phpWord->addSection([
                    'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(12.7),
                    'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.3)
                ]);
                $section7->addImage(
                    $leftupUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(3.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_LEFT,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginLeft'       => '-20',
                        'marginTop'        => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(-4),
                        'wrappingStyle' => 'behind',
                    )
                );
                $section7->addImage(
                    $rightdownUrl,
                    array(
                        'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(16.5)),
                        'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(3.877)),
                        'positioning'      => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
                        'posHorizontal'    => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
                        'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'posVerticalRel'   => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
                        'marginRight' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'marginBottom' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPixel(0)),
                        'posVertical'      => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_BOTTOM,
                        'wrappingStyle' => 'behind'
                    )
                );
                $section7->addTextBreak(1);
                $fancyTableStyleName = 'Fancy Table';
                $fancyTableStyle = array('borderSize' => 0,'Spacing'=> 0,'borderColor' => '#ffffff', 'afterSpacing'=>0, 'cellMargin' => 0, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 0);
                $fancyTableFirstRowStyle = array('borderBottomSize' => 1, 'borderBottomColor' => '#ffffff');



                $fancyTableCellStyle = array('valign' => 'center');
                $fancyTableFontStyle = array('valign' => 'center','borderBottomSize'=>1,'borderBottomColor'=>'#ffffff','alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,'spaceAfter' => 50,'spaceBefore' =>50);
                $phpWord-> addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
                $table = $section7->addTable($fancyTableStyleName);

                $section7 -> addText('职业倾向测评分析详解',[
                    'size'    => 18,
                    'name'=>'黑体',
                ],['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,
                    'spaceAfter'  => 200,                                                   //标题后部预留长度
                    'spaceBefore' => 0]);
                $table = $section7->addTable($fancyTableStyleName);
                $table->addRow();

                // 定义首行单元格样式
                $cellFirstRowStyle  =  array(
                    'borderBottomColor' => '000000', // 下边框颜色
                    'borderBottomSize' => 5, // 下边框大小
                    'borderBottomStyle' => 'single', // 下边框样式
                );
                $table->addCell(2000, $cellFirstRowStyle )->addText("类型",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5], $fancyTableFontStyle);
                $table->addCell(7600, $cellFirstRowStyle )->addText("详解",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5], $fancyTableFontStyle);

                $table->addRow();
                $table->addCell(2000,$fancyTableCellStyle)->addText("技术型（TF）",['name'=>'仿宋','size'=>12,'lineHeight'  => 2.5]);
                $table->addCell(7600,$fancyTableCellStyle)->addText("愿意展示自己的技能，并不断提升自己的技术，发挥更为专业的能力。希望通过施展自己的技能以获取别人认可，并乐于接受来自于专业领域的挑战。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000,$fancyTableCellStyle)->addText("管理型（GM）",['name'=>'仿宋','size'=>12,'lineHeight'  => 2.5]);
                $table->addCell(7600,$fancyTableCellStyle)->addText("追求组织中的管理职位，喜欢发号施令，领导众人，因此能够整合其他人的工作，并对组织中某项工作的绩效承担责任。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000, $fancyTableCellStyle)->addText("自主型（AU）",['name'=>'仿宋','size'=>12,'lineHeight'  => 2.5]);
                $table->addCell(7600)->addText("希望可以按照自己的方式工作和生活，总能提供足够的灵活性，无法忍受公司种种的约束，追求无拘无束。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000, $fancyTableCellStyle)->addText("安全型（SE）",['name'=>'仿宋','size'=>12,'lineHeight'  => 2.5]);
                $table->addCell(7600, $fancyTableCellStyle)->addText("喜欢稳定的职位，更为强调财务安全和就业安全，关注五险一金、养老金以及税收。对组织忠心耿耿，可以长久地从事某一行业。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000, $fancyTableCellStyle)->addText("创造型（EC）",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5]);
                $table->addCell(7600, $fancyTableCellStyle)->addText("冒险能力强，手腕强硬，希望向周围的人证明你有能力创建一家企业，并且能够全方位的学习，以此来实现自己的创业计划。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000, $fancyTableCellStyle)->addText("服务型（SV）",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5]);
                $table->addCell(7600, $fancyTableCellStyle)->addText("追求职业的价值取向，希望可以做一些利于他人的事，例如教育、环保、公益、慈善等。富有爱心，安全感和同情心。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                $table->addCell(2000, $fancyTableCellStyle)->addText("挑战型（CH）",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5]);
                $table->addCell(7600, $fancyTableCellStyle)->addText("总是跃跃欲试，希望战胜强硬的对手和克服面临的困难。多层次的、复杂的情况对你来说是一剂强心针，厌倦简单及重复的工作。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);

                $table->addRow();
                // 定义尾行单元格样式
                $cellLastRowStyle  =  array(
                    'borderBottomColor' => '000000', // 下边框颜色
                    'borderBottomSize' => 5, // 下边框大小
                    'borderBottomStyle' => 'single', // 下边框样式
                    'valign' => 'center',
                );
                $table->addCell(2000, $cellLastRowStyle)->addText("生活型（LS）",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5]);
                $table->addCell(7600, $cellLastRowStyle)->addText("家庭的需求和你个人的休息对你来说非常重要，因此你希望职业有足够的弹性，能让你平衡并整合个人的、家庭的和职业的矛盾，并且让他们协调统一地共同发展。",['name'=>'仿宋','size'=>12,'lineHeight'  => 1.5],['spaceAfter' => 200,'spaceBefore' =>100]);
            }
            // 职业锚  end

            $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');


            $rand = rand(1111,9999);
            $file_name = 'uploads/'.date('Ymd').'/'.$userInfo['grade'].'年级'.$userInfo['classed'].'班级'.$userInfo['nickname'].'.docx';

            $tmpPath ='uploads/'.date('Ymd').'/';
            if(!file_exists($tmpPath)){
                mkdir($tmpPath, 0777, true);
            }
            $objWriter->save($file_name);
            Db::name('batch_report')->where(['user_id'=>$user_id])->whereTime('createtime','today')->delete();
            $add = [
                'user_id'=>$user_id,
                'url'=>$file_name,
                'createtime'=>time(),
                'updatetime'=>time(),
            ];
            Db::name('batch_report')->insert($add);
            return;
        }
        catch (Exception $exception)
        {
            $this -> error($exception -> getMessage());
        }

    }

}
